package com.ttmusic.view;

import com.ttmusic.activitys.R;
import com.ttmusic.view.listeners.OnTouchingLetterChangedListener;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.graphics.drawable.ColorDrawable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;

/**
 * 自定义字母快速定位工具栏
 * @author wuchangyong
 *
 */
public class LetterBar extends View {

	// 触摸事件  
    private OnTouchingLetterChangedListener onTouchingLetterChangedListener;  
    // 26个字母  
    public static String[] letters = {"#", "A", "B", "C", "D", "E", "F", "G", "H", "I",  
            "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V",  
            "W", "X", "Y", "Z"};  
    private int choose = -1;// 选中  
//    private Paint paint = new Paint();  
  
    private TextView mTextDialog;

    /** 
     * 为SideBar设置显示字母的TextView 
     * @param mTextDialog 
     */  
    public void setTextView(TextView mTextDialog) {  
        this.mTextDialog = mTextDialog;  
    } 
	
	public LetterBar(Context context) {
		this(context,null);
	}
	
	public LetterBar(Context context, AttributeSet attrs) {
		this(context, attrs, 0);
	}
	
	public LetterBar(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
	}

	@SuppressLint("DrawAllocation")
	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		
		int height = getHeight();
		int width = getWidth();
		int singleHeight = height/letters.length;
		
		Paint paint = new Paint();
		
		for(int i=0;i<letters.length;i++){
			paint.setColor(Color.parseColor("#FFFFFF"));//设置画笔颜色 Color.rgb(33, 65, 98)
			paint.setTypeface(Typeface.DEFAULT_BOLD);	//设置加粗
			
			paint.setAntiAlias(true);
			paint.setTextSize(32);
			
			if(i==choose){
				paint.setColor(Color.parseColor("#3399ff"));  
                paint.setFakeBoldText(true);  
			}
			
			//计算坐标
			float x = width/2-paint.measureText(letters[i])/2;
			float y = (i+1)*singleHeight;
			
			canvas.drawText(letters[i], x, y, paint);
		}
	 
		//重置画笔
		paint.reset();
	}

	/** 
     * 向外公开的方法 
     *  
     * @param onTouchingLetterChangedListener 
     */  
    public void setOnTouchingLetterChangedListener(  
            OnTouchingLetterChangedListener onTouchingLetterChangedListener) {  
        this.onTouchingLetterChangedListener = onTouchingLetterChangedListener;  
    }

    /**
     * 发生事件后LetterBar对象本事要做的事情
     */
	@SuppressWarnings("deprecation")
	@Override
	public boolean dispatchTouchEvent(MotionEvent event) {
		float y=event.getY();
		int pos=(int) ((y/getHeight())*letters.length);
		final int oldchoose=choose;
		
		switch(event.getAction()){
			case MotionEvent.ACTION_UP:
				setBackgroundDrawable(new ColorDrawable(0x00000000));
				choose=-1;
				
				invalidate();
				if(mTextDialog!=null){
					mTextDialog.setVisibility(View.INVISIBLE);
				}
				break;
			default:
				setBackgroundResource(R.drawable.letterbar_background);
				if(oldchoose!=pos){
					if(pos>=0&&pos<letters.length){
						if(onTouchingLetterChangedListener!=null){
							onTouchingLetterChangedListener.onTouchingLetterChanged(letters[pos]);
						}
						
						if(mTextDialog!=null){
							mTextDialog.setText(letters[pos]);
							mTextDialog.setVisibility(View.VISIBLE);
						}
					}
					
					choose=pos;
					invalidate();
				}
				break;
		}
		
		return true;
	}
	
}
